<%@ page contentType="text/html; charset=UTF-8" errorPage="/jsp/error.jsp" %>
<%@ taglib uri="/WEB-INF/tlds/struts-html.tld"  prefix="html" %>
<%@ taglib uri="/WEB-INF/tlds/struts-bean.tld"  prefix="bean" %>
<%@ taglib uri="/WEB-INF/tlds/struts-logic.tld" prefix="logic" %>
<%@ taglib uri="/WEB-INF/tlds/custom-html.tld" prefix="chtml" %>

<%@ page import="java.util.ArrayList"%>
<%@ page import="com.erp.framework.util.user.User"%>
<%@ page import="com.erp.framework.util.CommonUtil"%>
<%@ page import="com.erp.framework.util.mainmenu.MenuGroup"%>
<%@ page import="com.erp.framework.util.mainmenu.MenuObject"%>
<%@ page import="com.erp.framework.util.mainmenu.MenuGroupDelegate"%>
<%@ page import="com.erp.app.common.ProjectConstants"%>
<%@ page import="com.erp.framework.util.logger.Logger"%>
<%@ page import="com.erp.app.login.web.MainMenuAction"%>
<%@ page import="com.erp.app.util.web.WebUtil"%>

<jsp:useBean id="mainMenuForm" scope="session" type="com.erp.app.login.web.MainMenuForm" />

<%
User editingUser = (User) session.getAttribute(ProjectConstants.SESSION_USER);
String menuMode = mainMenuForm.getMenuMode();

ArrayList menuList = mainMenuForm.getMenuList();
pageContext.setAttribute("menuList", menuList);
%>

<script language="javascript">
	function doLoad() {
		<%
		String lastAction = CommonUtil.nullToString(mainMenuForm.getPageEvent());
		String otherLastAction = CommonUtil.nullToString(mainMenuForm.getOtherPageEvent());
		String editDocType = CommonUtil.nullToString(mainMenuForm.getEditDocType());

		if (otherLastAction.equalsIgnoreCase(MainMenuAction.actionEditDoc)) {

//			if(editDocType.equalsIgnoreCase(ProjectConstants.MOD_PROJ_FABRIC_TASK)){
				out.println(WebUtil.openPopupFabricTaskDo(CommonUtil.nullToString(mainMenuForm.getEditTaskEnquiryEventSrc())));
System.out.println("JSP,*********************** pop up SO");
System.out.println("JSP,*********************** otherLastAction=" + otherLastAction);

				//out.println("window.open('salesContractMaintenance.do','docTrackDetailWin','width=800,height=600,resizable=yes,menubar=no,scrollbars=yes,status=yes');");
//				out.println("alert('alert');");
//				out.println("javascript:alert('javascript');");
//				out.println(WebUtil.openSoDo());
//			} else if(editDocType.equalsIgnoreCase(ProjectConstants.DOC_TYPE_PO)){
//System.out.println("JSP, ***********************pop up PO");
				//out.println("window.open('purchaseOrderMaintenance.do','enqPoWin','width=800,height=600,resizable=yes,menubar=no,scrollbars=yes,status=yes');");
//			}					
		} 

		if(lastAction.equalsIgnoreCase(MainMenuAction.actionHandleOsTask)) {
//System.out.println("JSP,*********************** MAINMENU pop up Outstanding Task");
//System.out.println("JSP,*********************** MAINMENU lastAction=" + lastAction);
			out.println(WebUtil.openOsTaskSummary());
		}

		%>
	}

	var isSubmitted = 0;
	function doSubmit(event, val) {
		if (isSubmitted == 0) {
			isSubmitted = 1;
			document.forms[0].pageEvent.value = event;
			if (val != null) {
				document.forms[0].pageEventSrc.value = val;
			}
			document.forms[0].submit();
		}
	}

	function doLogout(url)
	{
		var t = (new Date()).getTime();
		
		var wide = window.screen.availWidth - 10;
		var height = window.screen.availHeight - 50;

		window.open(url,t,'left=0, top=0, width='+wide+',height='+height+',resizable=yes,menubar=yes,toolbar=yes,location=yes,scrollbars=yes,status=yes');

		document.forms[0].target = t;
		window.opener = null;
		window.close();
	}

	function mouseOverMenu(img) {
		img.src = "images/menu_bg_selected.gif";
	}

	function mouseOutMenu(img) {
		img.src = "images/menu_bg.gif";
	}

<%--
	function mouseOverCategory(img) {
		img.src = "images/menu/bullet_over.png";
	}

	function mouseOutCategory(img) {
		img.src = "images/menu/bullet_out.png";
	}
--%>
	
</script>

<script language="javascript" src="<%=request.getContextPath()+ "/js/sys.js" %>">
</script>

<link rel="stylesheet" href="<%=request.getContextPath()+ "/css/style.css" %>" type="text/css">

<%--<body onLoad="javascript:doLoad();autofill_init();newCalendar()" text="#000000" leftmargin="0" topmargin="0" marginwidth="0" marginheight="0">--%>
<body onLoad="javascript:doLoad();autofill_init()" text="#000000" leftmargin="0" topmargin="0" marginwidth="0" marginheight="0" class="mainmenu_body">

<html:form action="/mainMenu">
<html:hidden property="pageEvent" />
<html:hidden property="pageEventSrc" />

<!-- ADVANCED MENU MODE -->
<logic:equal name="mainMenuForm" property="menuMode" value="<%=ProjectConstants.MENU_MODE_ADVANCED%>">

	<table border=0 cellpadding=2 align=left cellspacing=0 width="710">
	<tr>	
	
	<%
	MenuGroupDelegate menuDg = new MenuGroupDelegate();
	ArrayList allMenuList = menuDg.getAllMenuGroupList(editingUser.getCompanyCode(), editingUser.getUserId());
	pageContext.setAttribute("allMenuList", allMenuList);

	int imgWidth = 20;
	int imgHeight = 20;
	int menuImgWidth = 280;
	int menuImgHeight = 40;
	int txtWidth = 150;
	int txtHeight = 36;
	int i = 0;
	int noOfMenu = 2;
	int noOfItemsPerMenu = 1;
	int noOfMenuList = 0;
	
	
	%>
	<logic:iterate id="menuItem" name="menuList" indexId="idx" >
	<%
	MenuGroup parentMenuGroup = (MenuGroup) menuItem;
	%>
	<logic:equal name="menuItem" property="viewDisplay" value="<%=ProjectConstants.GENERIC_YES%>" >
		<logic:equal name="menuItem" property="menuType" value="M" >
			<logic:equal name="menuItem" property="parentMenuId" value="<%=ProjectConstants.PARENT_MENU_MENU_TAB%>" >
				<% noOfMenuList++; %>
	</logic:equal>
		</logic:equal>
			</logic:equal>
	</logic:iterate>						
	<% noOfMenuList=noOfMenuList/2; %>
	
	<!-- Displays Menu Folder -->

	<logic:iterate id="menuItem" name="menuList" indexId="idx" >
	
	<%
	if (i==0 || i==noOfMenuList) {		
		out.println("<td align=center valign=top>");		
	}
	%>
		
	<%
	MenuGroup parentMenuGroup = (MenuGroup) menuItem;
	%>
	<logic:equal name="menuItem" property="viewDisplay" value="<%=ProjectConstants.GENERIC_YES%>" >
		<logic:equal name="menuItem" property="menuType" value="M" >
			<logic:equal name="menuItem" property="parentMenuId" value="<%=ProjectConstants.PARENT_MENU_MENU_TAB%>" >
			<%
				MenuGroup menuGroup = (MenuGroup) menuItem;
				String url = menuGroup.getMenuUrl();
				String menuId = menuGroup.getMenuId();

				if (!menuId.equals(ProjectConstants.MOD_LOGOUT)) {
					url = "javascript:doSubmit('changeMenuTab', '"+menuId+"');";
				} else {
					url = "javascript:doLogout('"+url+"')";
				}
			%>			
		
			
			<table border=0 width=100% class='outline-table-menu' style="width:355px !important">
				<tr width=100%>
					<td colspan=<%=noOfItemsPerMenu%> align=left background="images/menu/wrapper_bg.png" width=100% nowrap style = "padding-left: 7px">
						<a href="#" border="0" onclick="switchMenu('<%=menuGroup.getName()%>');" title="expand/collapse menu" onmouseout="MM_swapImgRestore()" onmouseover="MM_swapImage('image01','','images/menu/btn_expand_collapse_over.png',1)"><img src="images/menu/btn_expand_collapse_out.png" name="expand/collapse menu" id="image01" class="float_right"/></a>						
						<font size=2 face=arial color="black" valign=bottom>
						<b><%=menuGroup.getName()%></b></font>					
					</td>
				</tr>
			</table>
			<div id="<%=menuGroup.getName()%>">
		  <table border=0 width=100% class='outline-table-menu'style="width:355px !important">
				<%
				String parentMenuId = parentMenuGroup.getMenuId();
				int j = 0;				
				%>
				
				<logic:iterate id="itemMenuItem" name="allMenuList" indexId="itemIdx" >
				<%
				MenuGroup itemMenuGroup = (MenuGroup) itemMenuItem;
				String menu = itemMenuGroup.getMenuId();
				%>
				<logic:equal name="itemMenuItem" property="rootMenuId" value="<%=ProjectConstants.PARENT_MENU_MENU_TAB%>">
					<logic:equal name="itemMenuItem" property="viewDisplay" value="<%=ProjectConstants.GENERIC_YES%>" >
						<logic:equal name="itemMenuItem" property="menuType" value="W" >
							<logic:equal name="itemMenuItem" property="parentMenuId" value="<%=parentMenuId%>">
								<tr>
								<td>
								<% if (itemMenuGroup.getMenuIcon() != null) { %>
									<a href="<%=itemMenuGroup.getMenuUrl()%>" class='tab_link'><img 
									src="menuIcon?menu=<%=itemMenuGroup.getMenuId()%>&group=<%=itemMenuGroup.getGroupId()%>" 
									width="<%=imgWidth%>" height="<%=imgHeight%>" border=0/><font size=2 face=arial><%=itemMenuGroup.getName()%></font></a>
								<% } else { %>
									<a href="<%=itemMenuGroup.getMenuUrl()%>" class='tab_link'>&raquo;&nbsp;&nbsp;<span class='mainmenu_text'><%=itemMenuGroup.getName()%></span></a>
								<% } %>									
								</td>
							  </tr>
								<%
								j++;
								%>	
							</logic:equal>
						</logic:equal>
					</logic:equal>
				</logic:equal>
				</logic:iterate>	
				
			</table>
			</div>
			<br>		
		<%
		i++;
		%>		
			</logic:equal>
		</logic:equal>
	</logic:equal>
	<%
	if (i==noOfMenuList) {	  
		out.println("</td>");				
	}
	%>
	</logic:iterate>
</tr>
	</table>

</logic:equal>
<!-- END OF ADVANCED MENU MODE -->

<!-- NORMAL MENU MODE -->
<logic:equal name="mainMenuForm" property="menuMode" value="<%=ProjectConstants.MENU_MODE_NORMAL%>">		
	<%
	int imgWidth = 48;
	int imgHeight = 36;
	int txtWidth = 150;
	int txtHeight = 36;
	%>

	<table border=0 cellpadding=0 cellspacing=0>
	<tr>
	<!-- Displays Menu Folder -->
	<logic:iterate id="menuItem" name="menuList" indexId="idx" >
	<logic:equal name="menuItem" property="viewDisplay" value="<%=ProjectConstants.GENERIC_YES%>" >
		<logic:equal name="menuItem" property="menuType" value="M" >
			<logic:equal name="menuItem" property="parentMenuId" value="<%=ProjectConstants.PARENT_MENU_MENU_TAB%>" >

			<%
				MenuGroup menuGroup = (MenuGroup) menuItem;
				String url = menuGroup.getMenuUrl();
				String menuId = menuGroup.getMenuId();

				if (!menuId.equals(ProjectConstants.MOD_LOGOUT)) {
					url = "javascript:doSubmit('changeMenuTab', '"+menuId+"');";
				} else {
					url = "javascript:doLogout('"+url+"')";
				}
			%>
			<!--SELECTED-->
			<logic:equal name="menuItem" property="menuId" value="<%= mainMenuForm.getSelectedMenu() %>" >
				<td align=center width=103 height=30 background="images/menu_bg_selected.gif">
					<font size=1 face=arial>
					<b><%=menuGroup.getName()%></b></font>
				</td>
			</logic:equal>
			<!--NOT SELECTED-->
			<logic:notEqual name="menuItem" property="menuId" value="<%= mainMenuForm.getSelectedMenu() %>" >
				<td align=center width=103 height=30 background="images/menu_bg.gif">
					<font size=1 face=arial>
						<a href="<%= url %>" class='tab_link'><%=menuGroup.getName()%></a></font>
				</td>
			</logic:notEqual>

			</logic:equal>
		</logic:equal>
	</logic:equal>
	</logic:iterate>
	</tr>
	</table>

	<html:errors />

	<table width="100%" border=0 cellpadding=0 cellspacing=0 valign="top">
	<tr valign="top">
	<%
	int j = 0;
	%>
	<logic:iterate id="menuItem" name="menuList" indexId="idx" >
	<%
		MenuGroup menuGroup = (MenuGroup) menuItem;
	%>
	<logic:equal name="menuItem" property="rootMenuId" value="<%=ProjectConstants.PARENT_MENU_MENU_TAB%>">
		<logic:equal name="menuItem" property="viewDisplay" value="<%=ProjectConstants.GENERIC_YES%>" >
			<logic:equal name="menuItem" property="menuType" value="W" >
			<% if (((j++ % (ProjectConstants.NO_ICONS_PER_LINE)) == 0) && (j > 0)) { %>
				</tr>
				<tr ><td colspan="2"><IMG NAME="space" SRC="images/spacer.gif" height="10" BORDER="0" ALT=""></td></tr>
				<tr valign="left">
				<td ><IMG NAME="space" SRC="images/spacer.gif" width="15" BORDER="0" ALT=""></td>
			<% } %>

			<td >
			<table border=0 cellpadding=0 cellspacing=0 valign="top">
			<tr>
				<td>
					<% if ( menuGroup.getMenuIcon() != null) { %>
						<a href="<%=menuGroup.getMenuUrl()%>" class='tab_link'><img 
						src="menuIcon?menu=<%=menuGroup.getMenuId()%>&group=<%=menuGroup.getGroupId()%>" 
						width="<%=imgWidth%>" height="<%=imgHeight%>" border=0/></a>
					<% } else { %>
						<a href="<%=menuGroup.getMenuUrl()%>" class='tab_link'><img 
						src="images/erp.jpg" width="<%=imgWidth%>" height="<%=imgHeight%>" border=0/></a>
					<% } %>
				</td>
				<td width="<%=txtWidth%>" height="<%=txtHeight%>" valign="center">
					<font size=1 color=#5F9EBB face=arial><%=menuGroup.getName()%></font>
				</td>
			</tr>	
			</table>

			</td>
			</logic:equal>
		</logic:equal>
	</logic:equal>
	</logic:iterate>
	</tr>
	</table>

</logic:equal>

<!-- END OF NORMAL MENU MODE -->

</html:form>

